$(function () {
    //调用搜索下拉框功能的方法
    loadSearch();
    //调用加载表格的方法
    tableData();
});

/**
 * 搜索框下拉框功能
 */
var loadSearch = function() {
    $.ajax({
        url:'sys/Statistics',
        type:'get',
        success:function(data){
            for (var i = 0; i < data.length; i++) {
                $("#fundName").append("<option value='"+data[i].fundId+"'>" + data[i].fundName+"</option>");
            }
            layui.use('form', function(){
                var form = layui.form;
                form.render();
            });
        }
    })
}

/**
 * 合并相同列的方法
 * @param fieldName
 * @param index
 * @returns {boolean}
 */
function alarmTableRowSpan(fieldName, index) {
    var fixedNode = document.getElementsByClassName("layui-table-body")[index - 1];
    if (!fixedNode) {
        return false;
    }
    var child = fixedNode.getElementsByTagName("td");
    var childFilterArr = [];
    // 获取data-field属性为fieldName的td
    for (var i = 0; i < child.length; i++) {
        if (child[i].getAttribute("data-field") == fieldName) {
            childFilterArr.push(child[i]);
        }
    }
    // 获取td的个数和种类
    var childFilterTextObj = {};
    for (var i = 0; i < childFilterArr.length; i++) {
        var childText = childFilterArr[i].textContent;
        if (childFilterTextObj[childText] == undefined) {
            childFilterTextObj[childText] = 1;
        } else {
            var num = childFilterTextObj[childText];
            childFilterTextObj[childText] = num * 1 + 1;
        }
    }
    // 给获取到的td设置合并单元格属性
    for (var key in childFilterTextObj) {
        var tdNum = childFilterTextObj[key];
        var canRowSpan = true;
        for (var i = 0; i < childFilterArr.length; i++) {
            if (childFilterArr[i].textContent == key) {
                if (canRowSpan) {
                    childFilterArr[i].setAttribute("rowspan", tdNum);
                    canRowSpan = false;
                } else {
                    childFilterArr[i].style.display = "none";
                }
            }
        }
    }
}

/*
 * 所有功能
 */
var tableData = function () {
    layui.config({
        base: 'static/module/'
    }).extend({
        tableMerge:'tableMerge'
    }).use(['table', 'tableMerge'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var tableMerge = layui.tableMerge;

        // 渲染表格
            table.render({ 
                elem: '#table1',
                url: 'sys/Statistics',
                page: false, //开启分页
                id:'dataTable',
                limit:100,
                cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    ,{field: 'fundId', title: '基金Id'}
                    ,{field: 'fundNo', title: '基金代码'}
                    ,{field: 'fundName', merge: true, title: '基金名'}
                    ,{field: 'securitiesType', merge: true, title: '项目类型'}
                    ,{field: 'securitiesName', title: '项目名'}
                    , {field: 'nav', title: '行情'}
                    , {field: 'share', title: '份额'}
                    , {field: 'turnover', title: '市值|余额|费用'}
                ]],
                done:function (res, curr, count) {
                    data = res.data;
                    var that = this.elem.next();
                    var fz = 0;
                    var heji = 0;
                    var jz = 0;
                    var fe = 0;
                    for (var i = 0;i<data.length;i++){
                        heji = heji + data[i].turnover;
                        if(data[i].securitiesType =='基金'){
                            fe = data[i].share;
                            $("#kc").html(fe);
                        }
                        if(data[i].flag ==2){
                            fz = fz + data[i].turnover;
                            console.info(fz)
                        }
                    }
                    jz = (heji-fz)/fe;
                    if(jz>0){
                        $("#jz").html(jz);
                    }else{
                        $("#jz").html('--');
                    }
                    if(heji==0){
                        $("#heji").html('--');
                    }else{
                        $("#heji").html(heji);
                    }
                    if(fz==0){
                        $("#fz").html('--');
                    }else{
                        $("#fz").html(fz);
                    }
                    data.forEach(function(item,index){
                    	console.log(item);
                    	 if(item.securitiesName =='基金'){
                             var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']").css("display","none");
                         }
                    })
                    
                    tableMerge.render(this);
                }
            });

       /* $("#fundSearchBtn").click(function () {
            //获得输入框的值
            var d=$("#fundName").val();
            table.reload("dataTable", {
                url: "sys/selectFVal"
                , where: {
                    fundId:d
                }
            });
            return false;
        });*/
    });
}


